home *** CD-ROM | disk | FTP | other *** search
/ EnigmA Amiga Run 1995 November / EnigmA AMIGA RUN 02 (1995)(G.R. Edizioni)(IT)[!][issue 1995-11][Skylink CD].iso / earcd / program / misc / bgui12.lha / demos / BGUIPlayer / Main.c < prev    next >
C/C++ Source or Header  |  1995-06-11  |  11KB  |  480 lines

  1. /*
  2.  *    MAIN.C
  3.  */
  4.  
  5. #include "BGUIPlayer.h"
  6.  
  7. Prototype ULONG ReportError( UBYTE *, UBYTE *, ... );
  8. Prototype struct MsgPort *SharedPort;
  9.  
  10. /*
  11.  *    Global data.
  12.  */
  13. struct Library *BGUIBase;
  14. Object           *CO_Broker;
  15. ULONG        BrokerSig;
  16. struct MsgPort *SharedPort;
  17.  
  18. /*
  19.  *    Show a BGUI requester. Used for
  20.  *    error reports and general information.
  21.  */
  22. ULONG ReportError( UBYTE *gads, UBYTE *string, ... )
  23. {
  24.     struct bguiRequest    req = { NULL };
  25.  
  26.     req.br_GadgetFormat    = gads;
  27.     req.br_TextFormat    = string;
  28.     req.br_Underscore    = '_';
  29.     req.br_Flags        = Player ? BREQF_CENTERWINDOW|BREQF_AUTO_ASPECT|BREQF_LOCKWINDOW : BREQF_CENTERWINDOW|BREQF_AUTO_ASPECT;
  30.  
  31.     return( BGUI_RequestA( Player, &req, ( ULONG * )( &string + 1 )));
  32. }
  33.  
  34. /*
  35.  *    Event handler. This routine will handle
  36.  *    all incoming message traffic from the
  37.  *    main window, edit window, timer and
  38.  *    commodity broker.
  39.  */
  40. VOID MsgHandler( void )
  41. {
  42.     ULONG            rc, tmp, sigrec, type, id;
  43.     struct Window           *sigwin;
  44.     BOOL            running = TRUE;
  45.  
  46.     do {
  47.         /*
  48.          *    Let's wait for a signal...
  49.          */
  50.         sigrec = Wait(( 1L << SharedPort->mp_SigBit ) | TimerMask | BrokerSig );
  51.  
  52.         /*
  53.          *    Timer message?
  54.          */
  55.         if ( sigrec & TimerMask ) {
  56.             /*
  57.              *    Valid timer message?
  58.              */
  59.             if ( CheckTimer()) {
  60.                 /*
  61.                  *    Reset timer/CD info and the
  62.                  *    pause toggle.
  63.                  */
  64.                 SCSI_ReadCDInfo( 1 );
  65.                 SetGadgetAttrs(( struct Gadget * )GO_Pause, Player, NULL, GA_Selected, Status == SCSI_STAT_PAUSED ? TRUE : FALSE, TAG_END );
  66.             }
  67.         }
  68.  
  69.         /*
  70.          *    Window message?
  71.          */
  72.         if ( sigrec & ( 1L << SharedPort->mp_SigBit )) {
  73.             while ( sigwin = GetSignalWindow( WO_Player )) {
  74.                 /*
  75.                  *    Read the CD information, don't reset the timer.
  76.                  */
  77.                 SCSI_ReadCDInfo( 0 );
  78.                 /*
  79.                  *    Which window was it?
  80.                  */
  81.                 if ( sigwin == Player ) {
  82.                     /*
  83.                      *    Poll messages.
  84.                      */
  85.                     while (( rc = HandleEvent( WO_Player )) != WMHI_NOMORE ) {
  86.                         /*
  87.                          *    One of the track selection buttons?
  88.                          */
  89.                         if ( rc >= 1 && rc <= 20 )
  90.                             /*
  91.                              *    Play the selected track.
  92.                              */
  93.                             SCSI_PlayAudio( rc );
  94.                         else {
  95.                             switch ( rc ) {
  96.  
  97.                                 case    ID_INQUIRE:
  98.                                     /*
  99.                                      *    Show device information.
  100.                                      */
  101.                                     SCSI_Inquire();
  102.                                     break;
  103.  
  104.                                     case    ID_EDIT:
  105.                                     /*
  106.                                      *    Open disk-edit window if
  107.                                      *    it is not open yet.
  108.                                      */
  109.                                     if ( ! Disk )
  110.                                         OpenDiskWindow();
  111.                                     break;
  112.  
  113.                                 case    ID_ABOUT:
  114.                                     /*
  115.                                      *    Show info about this program.
  116.                                      */
  117.                                     ReportError( "_OK", ISEQ_C ISEQ_B VERS " (" DATE ")\n\n"
  118.                                              ISEQ_N "By Jan van den Baard\n\n"
  119.                                              "Based on the SCSI-2 CD-ROM code from\n"
  120.                                              "MultiCDPlayer 1.0 by Boris Jakubaschk\n"
  121.                                              "and SCSIUtil 2.0 by Gary Duncan and Heiko Rath" );
  122.                                     break;
  123.  
  124.                                     case    WMHI_CLOSEWINDOW:
  125.                                     /*
  126.                                      *    Close the player window.
  127.                                      */
  128.                                     if ( Player )
  129.                                         ClosePlayerWindow();
  130.                                     break;
  131.  
  132.                                     case    ID_QUIT:
  133.                                     /*
  134.                                      *    Bye bye.
  135.                                      */
  136.                                     running = FALSE;
  137.                                     break;
  138.  
  139.                                 case    ID_HIDE:
  140.                                     /*
  141.                                      *    Hide all widows.
  142.                                      */
  143.                                     if ( Player ) ClosePlayerWindow();
  144.                                     if ( Disk   ) CloseDiskWindow();
  145.                                     break;
  146.  
  147.                                 case    ID_PLAY:
  148.                                     /*
  149.                                      *    Start/Continue playing.
  150.                                      */
  151.                                     if ( Status == SCSI_STAT_PAUSED       ) SCSI_PauseResume();
  152.                                     else if ( Status != SCSI_STAT_PLAYING ) SCSI_PlayAudio( 1 );
  153.                                     break;
  154.  
  155.                                 case    ID_NEXT:
  156.                                     /*
  157.                                      *    Play next track.
  158.                                      */
  159.                                     if ( Status == SCSI_STAT_PLAYING ) SCSI_PlayAudio( Track + 1 );
  160.                                     break;
  161.  
  162.                                 case    ID_PREV:
  163.                                     /*
  164.                                      *    When less than one second played we
  165.                                      *    play the previous track. Otherwise we
  166.                                      *    restart the current track. Just like a
  167.                                      *    regular CD player ;) Mine does...
  168.                                      */
  169.                                     if ( Status == SCSI_STAT_PLAYING ) {
  170.                                         if ( TimeIDA == 0 && TimeIDB == 0 ) SCSI_PlayAudio( Track - 1 );
  171.                                         else                    SCSI_PlayAudio( Track );
  172.                                     }
  173.                                     break;
  174.  
  175.                                 case    ID_PAUSE:
  176.                                     /*
  177.                                      *    Pause/Continue playing.
  178.                                      */
  179.                                     SCSI_PauseResume();
  180.                                     break;
  181.  
  182.                                 case    ID_STOP:
  183.                                     /*
  184.                                      *    Stop playing.
  185.                                      */
  186.                                     SCSI_Stop();
  187.                                     break;
  188.  
  189.                                 case    ID_EJECT:
  190.                                     /*
  191.                                      *    Open drive door and reset
  192.                                      *    visuals.
  193.                                      */
  194.                                     SCSI_Eject();
  195.                                     TrackID = IndexID = TimeIDA = TimeIDB = TogoIDA = TogoIDB = 0;
  196.                                     break;
  197.  
  198.                                 case    ID_FORWARD:
  199.                                     /*
  200.                                      *    Jump ahead 10 seconds (10 frames.)
  201.                                      */
  202.                                     if ( Status == SCSI_STAT_PLAYING ) SCSI_Jump( 750 );
  203.                                     break;
  204.  
  205.                                 case    ID_BACKWARD:
  206.                                     /*
  207.                                      *    Jump back 10 seconds (10 frames.)
  208.                                      */
  209.                                     if ( Status == SCSI_STAT_PLAYING ) SCSI_Jump( -750 );
  210.                                     break;
  211.  
  212.                                     case    ID_VOLUME:
  213.                                     /*
  214.                                      *    Adjust output volume.
  215.                                      */
  216.                                     GetAttr( SLIDER_Level, GO_Volume, &tmp );
  217.                                     SCSI_SetVolume( tmp, tmp, tmp, tmp );
  218.                                     break;
  219.                             }
  220.                         }
  221.                     }
  222.                 }
  223.  
  224.                 /*
  225.                  *    Disk editor message?
  226.                  */
  227.                 if ( sigwin == Disk ) {
  228.                     UBYTE        *name, *str;
  229.                     /*
  230.                      *    Poll messages.
  231.                      */
  232.                     while (( rc = HandleEvent( WO_Disk )) != WMHI_NOMORE ) {
  233.                         switch ( rc ) {
  234.  
  235.                             case    WMHI_CLOSEWINDOW:
  236.                                 /*
  237.                                  *    Close the disk editor.
  238.                                  */
  239.                                 if ( Disk ) {
  240.                                     CloseDiskWindow();
  241.                                     goto setDisplay;
  242.                                 }
  243.                                 break;
  244.  
  245.                             case    ID_DISKLIST:
  246.                                 /*
  247.                                  *    Setup the selected track.
  248.                                  */
  249.                                 if ( name = ( UBYTE * )FirstSelected( GO_DiskList ))
  250.                                     SetGadgetAttrs(( struct Gadget * )GO_DiskTrack, Disk, NULL, STRINGA_TextVal, name, TAG_END );
  251.                                 break;
  252.  
  253.                             case    ID_TRACK:
  254.                                 /*
  255.                                  *    Change the track name in the list.
  256.                                  */
  257.                                 if ( name = ( UBYTE * )FirstSelected( GO_DiskList )) {
  258.                                     GetAttr( STRINGA_TextVal, GO_DiskTrack, ( ULONG * )&str );
  259.                                     strcpy( name, str );
  260.                                     BGUI_DoGadgetMethod( GO_DiskList, Disk, NULL, LVM_REPLACE, NULL, name, name );
  261.                                 }
  262.                                 break;
  263.  
  264.                             case    ID_SAVEDISK:
  265.                                 /*
  266.                                  *    Get the data from the string objects.
  267.                                  */
  268.                                 GetAttr( STRINGA_TextVal, GO_Disk, ( ULONG * )&name );
  269.                                 strcpy( DiskName, name );
  270.                                 GetAttr( STRINGA_TextVal, GO_Artist, ( ULONG * )&name );
  271.                                 strcpy( Artist, name );
  272.                                 GetAttr( STRINGA_TextVal, GO_DiskLabel, ( ULONG * )&name );
  273.                                 strcpy( DiskLabel, name );
  274.                                 /*
  275.                                  *    Save the file to disk.
  276.                                  */
  277.                                 SaveDiskFile();
  278.                                 /*
  279.                                  *    Close the disk editor window.
  280.                                  */
  281.                                 CloseDiskWindow();
  282.                                 /*
  283.                                  *    Setup visuals.
  284.                                  */
  285.                                 setDisplay:
  286.                                 SetGadgetAttrs(( struct Gadget * )GO_Title,     Player, NULL, INFO_TextFormat, DiskName, TAG_END );
  287.                                 SetGadgetAttrs(( struct Gadget * )GO_TrackTitle, Player, NULL, INFO_TextFormat, Status == SCSI_STAT_STOPPED ? Artist : &DiskTracks[ Track - 1 ][ 0 ], TAG_END );
  288.                                 break;
  289.                         }
  290.                     }
  291.                 }
  292.             }
  293.         }
  294.  
  295.         /*
  296.          *    Commodity message?
  297.          */
  298.         if ( sigrec & BrokerSig ) {
  299.             /*
  300.              *    Get messages from the broker.
  301.              */
  302.             while ( MsgInfo( CO_Broker, &type, &id, NULL ) != CMMI_NOMORE ) {
  303.                 /*
  304.                  *    Evaluate message.
  305.                  */
  306.                 switch ( type ) {
  307.  
  308.                     case    CXM_IEVENT:
  309.                         switch ( id ) {
  310.                             case    CXK_POPUP:
  311.                                 /*
  312.                                  *    Popup the player window.
  313.                                  */
  314.                                 if ( ! Player )
  315.                                     OpenPlayerWindow( TRUE );
  316.                                 break;
  317.                         }
  318.                         break;
  319.  
  320.                     case    CXM_COMMAND:
  321.                         switch ( id ) {
  322.                                 case    CXCMD_KILL:
  323.                                     /*
  324.                                      *    Bye bye.
  325.                                      */
  326.                                     running = FALSE;
  327.                                     break;
  328.  
  329.                                 case    CXCMD_DISABLE:
  330.                                     /*
  331.                                      *    Disable the broker.
  332.                                      */
  333.                                     DisableBroker( CO_Broker );
  334.                                     break;
  335.  
  336.                                 case    CXCMD_ENABLE:
  337.                                     /*
  338.                                      *    Enable the broker.
  339.                                      */
  340.                                     EnableBroker( CO_Broker );
  341.                                     break;
  342.  
  343.                                 case    CXCMD_UNIQUE:
  344.                                 case    CXCMD_APPEAR:
  345.                                     /*
  346.                                      *    Open the player window.
  347.                                      */
  348.                                     if ( ! Player )
  349.                                         OpenPlayerWindow( TRUE );
  350.                                     break;
  351.  
  352.                                 case    CXCMD_DISAPPEAR:
  353.                                     /*
  354.                                      *    Close all windows.
  355.                                      */
  356.                                     if ( Player ) ClosePlayerWindow();
  357.                                     if ( Disk   ) CloseDiskWindow();
  358.                                     break;
  359.                         }
  360.                         break;
  361.                 }
  362.             }
  363.         }
  364.     } while ( running );
  365. }
  366.  
  367. /*
  368.  *    Setup the program.
  369.  */
  370. static BOOL SetupBGP( void )
  371. {
  372.     ULONG            added;
  373.  
  374.     /*
  375.      *    Open BGUI.
  376.      */
  377.     if ( BGUIBase = OpenLibrary( BGUINAME, BGUIVERSION )) {
  378.         /*
  379.          *    Create commodity broker.
  380.          */
  381.         CO_Broker = CommodityObject,
  382.             COMM_Name,        VERS,
  383.             COMM_Title,        VERS " (" DATE ")",
  384.             COMM_Description,    "Compact Disk Digital Audio Player.",
  385.             COMM_Priority,        0,
  386.             COMM_ShowHide,        TRUE,
  387.         EndObject;
  388.         /*
  389.          *    OK?
  390.          */
  391.         if ( CO_Broker ) {
  392.             /*
  393.              *    Load preferences.
  394.              */
  395.             LoadConfig();
  396.             /*
  397.              *    Get commadity signal.
  398.              */
  399.             GetAttr( COMM_SigMask, CO_Broker, &BrokerSig );
  400.             /*
  401.              *    Add popup key.
  402.              */
  403.             added = AddHotkey( CO_Broker, Popkey, CXK_POPUP, 0L );
  404.             /*
  405.              *    OK?
  406.              */
  407.             if ( added == 1 ) {
  408.                 /*
  409.                  *    Setup the SCSI CD-ROM communication stuff.
  410.                  */
  411.                 if ( SetupSCSI( DeviceName, DevID )) {
  412.                     /*
  413.                      *    Is the device a CD-ROM?
  414.                      */
  415.                     if ( SCSI_IsCDRom()) {
  416.                         /*
  417.                          *    Setup the timer.deice.
  418.                          */
  419.                         if ( SetupTimer()) {
  420.                             /*
  421.                              *    Enable the broker.
  422.                              */
  423.                             EnableBroker( CO_Broker );
  424.                             /*
  425.                              *    Create the shared message port.
  426.                              */
  427.                             if ( SharedPort = CreateMsgPort())
  428.                                 return( TRUE );
  429.                             else
  430.                                 ReportError( "_OK", "Can't create a message port." );
  431.                             KillTimer();
  432.                         }
  433.                     } else
  434.                         ReportError( "_OK", "The device can not be established\nto be a CD-ROM player!" );
  435.                     EndSCSI();
  436.                 }
  437.             } else
  438.                 ReportError( "_OK", "Unable to setup commodity hotkey!" );
  439.             DisposeObject( CO_Broker );
  440.         } else
  441.             ReportError( "_OK", "Unable to create a commodity object!" );
  442.         CloseLibrary( BGUIBase );
  443.     } else
  444.         Printf( "Unable to open the bgui.library V37 or better!\n" );
  445.     return( FALSE );
  446. }
  447.  
  448. /*
  449.  *    Close resources.
  450.  */
  451. static VOID CloseBGP( void )
  452. {
  453.     if ( WO_Disk    ) DisposeObject( WO_Disk    );
  454.     if ( WO_Player    ) DisposeObject( WO_Player  );
  455.     if ( CO_Broker    ) DisposeObject( CO_Broker  );
  456.     EndSCSI();
  457.     KillTimer();
  458.     if ( SharedPort ) DeleteMsgPort( SharedPort );
  459.     CloseLibrary( BGUIBase );
  460. }
  461.  
  462. /*
  463.  *    Main entry point. Dunno if SAS takes this...
  464.  */
  465. int _main( int ac, char *av )
  466. {
  467.     if ( SetupBGP()) {
  468.         SCSI_ReadCDInfo( 1 );
  469.         OpenPlayerWindow( Popup );
  470.         MsgHandler();
  471.         CloseBGP();
  472.     }
  473.     _exit( 0 );
  474.  
  475. #ifdef _DCC
  476. extern void _waitwbmsg( void );
  477.     _waitwbmsg();
  478. #endif
  479. }
  480.